import Vue from 'vue'
import Vuex from 'vuex'
import {
  tbDictDefinitionQuery,
  tbDictDefinitionQueryConstantStrList,
  tbDictDefinitionUpdateByConstantCode,
  openApiTbDictDefinitionQueryConstantStrList,
} from '@/api/admin/system/dictionary'
import {
  tbDictDetailUpdate,
  tbDictDetailQueryByCode,
  tbDictDetailOpenApiQueryByCode,
} from '@/api/admin/system/dictionaryDet.js'
import { tbSysAttachmentDownload } from '@/api/http'
Vue.use(Vuex)

const state = {}

const getters = {}

const actions = {
  //下载文件
  async downloadFileTbNetDisk({ commit }, id) {
    let res = await tbSysAttachmentDownload(id)
    return res.data
  },
  async queryTbDictDefinition({ commit }, listQuery) {
    let res = await tbDictDefinitionQuery(listQuery)
    return res.data.code == 0
      ? res.data.data
      : Promise.reject(new Error('faile'))
  },
  //通过code取数据字典的值(前台页面调用)
  async getDictionaryVal({ commit }, code) {
    let res = await openApiTbDictDefinitionQueryConstantStrList({ code })
    return res.data.code == 0
      ? res.data.data
      : Promise.reject(new Error('faile'))
  },
  //通过code取数据字典的值(后台页面调用)
  async getDictByCode({ commit }, code) {
    let res = await tbDictDefinitionQueryConstantStrList({ code })
    return res.data.code == 0
      ? res.data.data[0]
      : Promise.reject(new Error('faile'))
  },
  //保存数据字典信息
  async saveDicByCode({ commit }, { code, name }) {
    if (!name) return
    let res = await tbDictDefinitionUpdateByConstantCode({ code, name })
    return res.data.code == 0 ? 'ok' : Promise.reject(new Error('faile'))
  },
  //修改子集
  async updateTbDictDetail({ commit }, data) {
    let res = await tbDictDetailUpdate(data)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  //通过code查询枚举子集
  async queryTbDictDetailByCode({ commit }, data) {
    let res = await tbDictDetailQueryByCode(data)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  //通过code查询枚举子集
  async queryOpenApiTbDictDetailByCode({ commit }, data) {
    let res = await tbDictDetailOpenApiQueryByCode(data)
    return res.data.code == 0
      ? res.data.data
      : Promise.reject(new Error('faile'))
  },
}

const mutations = {}

export default {
  state,
  getters,
  actions,
  mutations,
}
